﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using csvviewer.logic.data;

namespace csvviewer.logic
{
    public class Format_table
    {
        public void Process(PageOfRecords page)
        {
            var table = new List<string>();

            foreach(var r in page.Records)
                table.Add(RecordToTableRow(r, "|"));
            table.Insert(1, RecordToTableRow(new CsvRecord { Values = CreateHeaderUnderline(page.Records.First().Values) }, "+"));
			table.Add(string.Format("Page {0} of {1}", page.PageInfo.PageNo, page.PageInfo.NumberOfPages));
			
            this.Table(table);
        }



        public event Action<IEnumerable<string>> Table;

        
        private string RecordToTableRow(CsvRecord record, string delimiter)
        {
            return string.Join(delimiter, record.Values) + delimiter;
        }


        private string[] CreateHeaderUnderline(IEnumerable<string> values)
        {
            return values.Select(v => new string('-', v.Length)).ToArray();
        }
    }
}
